﻿using Eqwww.Code;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace Eqwww.Config
{
    public class DbBackupConfigProvider:IDbBackupConfigProvider
    {
        private IConfiguration _configuration;
        private string _basePath;

        public DbBackupConfigProvider(IWebHostEnvironment env)
        {
            var basePath = env.WebRootPath + "/configs/";
            _basePath = basePath;
            var builder = new ConfigurationBuilder()
                .SetBasePath(basePath)
                .AddJsonFile("dbbackupconfig.json", optional: false, reloadOnChange: true);
            _configuration = builder.Build();
        }

        public DbBackupConfig Load()
        {
            var config = _configuration.Get<DbBackupConfig>();
            return config;
        }

        public void Save(DbBackupConfig config)
        {
            Eqwww.Code.FileHelper.CreateFile(_basePath + "dbbackupconfig.json", config.ToJson());
        }
    }
}
